//
//  CompanyDetailBaseInfoView.m
//  Btpin
//
//  Created by Vic on 2019/3/19.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CompanyDetailBaseInfoView.h"
#import "CompanyTagsView.h"

@interface CompanyDetailBaseInfoView ()
{
    UILabel *companyInfoLbl;
    UILabel *descriptionLbl;
    
    UILabel *descriptionInfoLbl;
    UIButton *moreBtn;
    UIView *descriptionLine;
    UILabel *certLbl;
    UIImageView *certIconImgView;
    UIImageView *certImgView;
    UIView *certBgView;
    UIView *certLine;
    UIView *contactBgView;
    
    CompanyInfoModel *dataModel;
    
    NSString *cert_pic; // 营业执照图片
}
@end

@implementation CompanyDetailBaseInfoView

- (instancetype)initWithFrame:(CGRect)frame target:(id)target info:(CompanyInfoModel *)model {
    self = [super initWithFrame:frame];
    if (self) {
        dataModel = [[CompanyInfoModel alloc] init];
        dataModel = model;
        [self initUIWithInfo:model];
        self.delegate = target;
    }
    return self;
}
- (void)initUIWithInfo:(CompanyInfoModel *)model {
    self.userInteractionEnabled = YES;
    self.backgroundColor = COLOR_OF_WHITE;
    UIImageView *bgImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(0, 0, WIDTH, 200*Scale) imageName:@"company_detail_bg"];
    [self addSubview:bgImgView];
    
    UIImageView *logoImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(WIDTH*0.5-37.5*Scale, EndY(bgImgView)-50*Scale, 75*Scale, 75*Scale) imageName:@"logo_bg"];
    logoImgView.backgroundColor = THEMECOLOR_OF_GRAY;
    logoImgView.contentMode = UIViewContentModeScaleAspectFill;
    logoImgView.v_cornerRadius =75*Scale/2;
    [self addSubview:logoImgView];
    if (model.logo && model.logo.length) {
        [logoImgView sd_setImageWithURL:[NSURL URLWithString:model.logo]];
    }
    
    companyInfoLbl = [UIToolsControl initLabelWithFrame:CGRectMake(LeftSpace, EndY(bgImgView)+45*Scale, WIDTH-30*Scale, 30*Scale) text:@"公司信息" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    companyInfoLbl.font = FontBold(Font_20);
    [self addSubview:companyInfoLbl];
    
    UILabel *natureLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(companyInfoLbl), ViewWidth(companyInfoLbl), 20*Scale) text:[NSString stringWithFormat:@"性质：%@", model.nature_cn] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [self addSubview:natureLbl];
    
    UILabel *scaleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(natureLbl), ViewWidth(companyInfoLbl), ViewHeight(natureLbl)) text:[NSString stringWithFormat:@"规模：%@", model.scale_cn] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [self addSubview:scaleLbl];
    
    UILabel *tradeLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(scaleLbl), ViewWidth(companyInfoLbl), ViewHeight(natureLbl)) text:[NSString stringWithFormat:@"行业：%@", model.trade_cn] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [self addSubview:tradeLbl];
    
    UILabel *financeLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(tradeLbl), ViewWidth(companyInfoLbl), ViewHeight(natureLbl)) text:[NSString stringWithFormat:@"融资：%@", model.finance_cn] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [self addSubview:financeLbl];
    
    CompanyTagsView *tagsBgView = [[CompanyTagsView alloc] initWithFrame:CGRectMake(LeftSpace, EndY(financeLbl)+15*Scale, WIDTH-30*Scale, 110*Scale) dataArray:model.tags];
    [self addSubview:tagsBgView];
    [tagsBgView borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeBottom)];
    
    descriptionLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(tagsBgView)+15*Scale, ViewWidth(companyInfoLbl), ViewHeight(companyInfoLbl)) text:@"公司介绍" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    descriptionLbl.font = FontBold(Font_20);
    [self addSubview:descriptionLbl];
    
    descriptionInfoLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(descriptionLbl)+10*Scale, ViewWidth(companyInfoLbl), (model.descriptionHeight>100*Scale)?80*Scale:model.descriptionHeight) text:nil textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    descriptionInfoLbl.numberOfLines = 0;
    descriptionInfoLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [self addSubview:descriptionInfoLbl];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = LineSpacingHeight;
    descriptionInfoLbl.attributedText = [[NSAttributedString alloc] initWithString:model.descriptionStr attributes:@{NSFontAttributeName:Font(Font_13), NSParagraphStyleAttributeName:paraStyle}];
    
    moreBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, EndY(descriptionInfoLbl), WIDTH, 60*Scale) imageName:nil target:self action:@selector(moreBtnTap) title:@"展开更多信息" titleColor:THEMECOLOR_OF_TEXT_cccccc];
    moreBtn.titleLabel.font = Font(Font_13);
    moreBtn.backgroundColor = COLOR_OF_WHITE;
    moreBtn.hidden = YES;
    [self addSubview:moreBtn];
    if ((model.descriptionHeight>100*Scale)) {
        moreBtn.hidden = NO;
        descriptionLine = [UIToolsControl initViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(moreBtn), WIDTH-30*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
    } else {
        moreBtn.hidden = YES;
        descriptionLine = [UIToolsControl initViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(descriptionInfoLbl)+15*Scale, WIDTH-30*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
    }
    [self addSubview:descriptionLine];
    
    certLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(descriptionLine)+15*Scale, 90*Scale, ViewHeight(companyInfoLbl)) text:@"营业执照" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    certLbl.font = FontBold(Font_20);
    [self addSubview:certLbl];
    certIconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(110*Scale, EndY(descriptionLine)+23*Scale, 17*Scale, 15*Scale) imageName:@"cert_icon"];
    [self addSubview:certIconImgView];
    if ([model.show_cert integerValue]) {
        certImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(certLbl)+10*Scale, 110*Scale, 110*Scale) imageName:nil];
        if (model.cert_pic && model.cert_pic.length) {
            [certImgView sd_setImageWithURL:[NSURL URLWithString:model.cert_pic]];
            cert_pic = model.cert_pic;
        }
        [self addSubview:certImgView];
        
        certLine = [UIToolsControl initViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(certImgView)+10*Scale, WIDTH-30*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
        [self addSubview:certLine];
        
        UIButton *btn = [UIToolsControl initButtonWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(certLbl)+10*Scale, 110*Scale, 110*Scale) imageName:nil target:self action:@selector(picBtnTap) title:nil titleColor:nil];
        [self addSubview:btn];
    } else {
        certBgView = [UIToolsControl initViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(certLbl)+10*Scale, WIDTH-30*Scale, 40*Scale) backgroundColor:COLOR_OF_WHITE];
        [self addSubview:certBgView];
        UIImageView *lockIconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(0, 7*Scale, 16*Scale, 20*Scale) imageName:@"lock_icon"];
        [certBgView addSubview:lockIconImgView];
        UILabel *lockLbl = [UIToolsControl initLabelWithFrame:CGRectMake(25*Scale, 0, ViewWidth(certBgView), ViewHeight(certBgView)-8*Scale) text:@"仅限简历已认证的用户查看" textColor:THEMECOLOR_OF_TEXT_cccccc font:Font_13];
        [certBgView addSubview:lockLbl];
        
        certLine = [UIToolsControl initViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(certBgView)+10*Scale, WIDTH-30*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
        [self addSubview:certLine];
    }
    
    contactBgView = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(certLine), WIDTH, 250*Scale) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:contactBgView];
    UILabel *contactTitleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), 15*Scale, ViewWidth(companyInfoLbl), ViewHeight(companyInfoLbl)) text:@"联系方式" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    contactTitleLbl.font = FontBold(Font_20);
    [contactBgView addSubview:contactTitleLbl];
    
    UILabel *phoneLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(contactTitleLbl), ViewWidth(companyInfoLbl), ViewHeight(natureLbl)) text:[NSString stringWithFormat:@"电话：%@", model.telephone] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [contactBgView addSubview:phoneLbl];
    if (![model.show_telephone integerValue]) {
        NSString *phoneText = [NSString stringWithFormat:@"电话：%@", model.telephone];
        NSString *phoneAllText = [NSString stringWithFormat:@"%@(仅限简历已认证的用户查看)", phoneText];
        NSMutableAttributedString *phoneAtt = [[NSMutableAttributedString alloc] initWithString:phoneAllText];
        [phoneAtt addAttributes:@{NSFontAttributeName:Font(Font_12), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_cccccc} range:NSMakeRange(0, phoneAllText.length)];
        [phoneAtt addAttributes:@{NSFontAttributeName:Font(Font_13), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_666666} range:NSMakeRange(0, phoneText.length)];
        phoneLbl.attributedText = phoneAtt;
    }
    UILabel *emailLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(phoneLbl), ViewWidth(companyInfoLbl), ViewHeight(natureLbl)) text:[NSString stringWithFormat:@"邮箱：%@", model.email] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [contactBgView addSubview:emailLbl];
    if (![model.show_email integerValue]) {
        NSString *emailText = [NSString stringWithFormat:@"邮箱：%@", model.email];
        NSString *emialAllText = [NSString stringWithFormat:@"%@(仅限简历已认证的用户查看)", emailText];
        NSMutableAttributedString *emailAtt = [[NSMutableAttributedString alloc] initWithString:emialAllText];
        [emailAtt addAttributes:@{NSFontAttributeName:Font(Font_12), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_cccccc} range:NSMakeRange(0, emialAllText.length)];
        [emailAtt addAttributes:@{NSFontAttributeName:Font(Font_13), NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_666666} range:NSMakeRange(0, emailText.length)];
        emailLbl.attributedText = emailAtt;
    }
    UILabel *addressLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(emailLbl), ViewWidth(companyInfoLbl), ViewHeight(natureLbl)) text:[NSString stringWithFormat:@"地址：%@", model.address] textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    [contactBgView addSubview:addressLbl];
    
    UIView *contactLine = [UIToolsControl initViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(addressLbl)+15*Scale, WIDTH-30*Scale, lineH) backgroundColor:THEMECOLOR_OF_TEXT_cccccc];
    [contactBgView addSubview:contactLine];
    
    UIImageView *cautionIconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(contactLine)+15*Scale, 20*Scale, 16*Scale) imageName:@"caution_icon"];
    [contactBgView addSubview:cautionIconImgView];
    NSString *contactText = @"以担保或任何理由索取财务，扣押证件，均涉嫌违法，请提高警惕";
    CGFloat contactHeight = [UIToolsControl textHeightWithLineSpacing:contactText size:CGSizeMake(WIDTH-60*Scale, MAXFLOAT) font:Font_13]+1;
    UILabel *cautionLbl = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(cautionIconImgView)+10*Scale, StartY(cautionIconImgView)-1, WIDTH-60*Scale, contactHeight) text:contactText textColor:THEMECOLOR_OF_ORANGE font:Font_13];
    [contactBgView addSubview:cautionLbl];
    NSMutableAttributedString *cautionAtt = [[NSMutableAttributedString alloc] initWithString:contactText];
    cautionLbl.attributedText = cautionAtt;
    cautionLbl.numberOfLines = 0;
    cautionLbl.lineBreakMode = NSLineBreakByWordWrapping;
    NSMutableParagraphStyle *cautionParaStyle = [[NSMutableParagraphStyle alloc] init];
    cautionParaStyle.lineSpacing = LineSpacingHeight;
    cautionLbl.attributedText = [[NSAttributedString alloc] initWithString:contactText attributes:@{NSFontAttributeName:Font(Font_13), NSParagraphStyleAttributeName:cautionParaStyle}];
    
//    UIView *bottomLine = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(cautionLbl)+20*Scale, WIDTH, 10*Scale) backgroundColor:THEMECOLOR_OF_GRAY];
//    [contactBgView addSubview:bottomLine];
//
//    UILabel *bottomLbl = [UIToolsControl initLabelWithFrame:CGRectMake(StartX(companyInfoLbl), EndY(bottomLine), WIDTH-30*Scale, 60*Scale) text:@"该公司正在招聘的职位" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_15];
//    [contactBgView addSubview:bottomLbl];
}
- (void)showAllDescription {
    moreBtn.hidden = YES;
    descriptionInfoLbl.frame = CGRectMake(StartX(companyInfoLbl), EndY(descriptionLbl)+10*Scale, ViewWidth(companyInfoLbl), dataModel.descriptionHeight);
    descriptionLine.frame = CGRectMake(StartX(companyInfoLbl), EndY(descriptionInfoLbl)+10*Scale, WIDTH-30*Scale, lineH);
    
    certLbl.frame = CGRectMake(StartX(companyInfoLbl), EndY(descriptionLine)+15*Scale, 90*Scale, ViewHeight(companyInfoLbl));
    certIconImgView.frame = CGRectMake(110*Scale, EndY(descriptionLine)+23*Scale, 17*Scale, 15*Scale);
    if ([dataModel.show_cert integerValue]) {
        certImgView.frame = CGRectMake(StartX(companyInfoLbl), EndY(certLbl)+10*Scale, 110*Scale, 110*Scale);
        certLine.frame = CGRectMake(StartX(companyInfoLbl), EndY(certImgView)+10*Scale, WIDTH-30*Scale, lineH);
    } else {
        certBgView.frame = CGRectMake(StartX(companyInfoLbl), EndY(certLbl)+10*Scale, WIDTH-30*Scale, 40*Scale);
        certLine.frame = CGRectMake(StartX(companyInfoLbl), EndY(certBgView)+10*Scale, WIDTH-30*Scale, lineH);
    }
    contactBgView.frame = CGRectMake(0, EndY(certLine), WIDTH, 250*Scale);
}
- (void)moreBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(companyDetailBaseInfoViewMoreTap)]) {
        [self.delegate companyDetailBaseInfoViewMoreTap];
    }
}
- (void)picBtnTap {
    if (cert_pic&&cert_pic.length) {
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_CertPic_LookImg object:nil userInfo:@{@"url":cert_pic}];
    }
}

@end
